/**
 * 音乐平台URL配置和示例
 */

// 可用的测试音频URL
export const testAudioUrls = [
  {
    name: 'SoundJay - Magic Chime',
    url: 'https://www.soundjay.com/misc/sounds/magic-chime-02.mp3',
    platform: 'direct',
    description: '直接音频文件链接 (MP3格式)'
  },
  {
    name: 'Archive.org - Classical Music',
    url: 'https://archive.org/download/testmp3testfile/mpthreetest.mp3',
    platform: 'direct',
    description: 'Archive.org 测试音频文件'
  },
  {
    name: 'Internet Archive - Audio Sample',
    url: 'https://ia902606.us.archive.org/35/items/shortpoetry_047_librivox/song_cjrg_teasdale_64kb.mp3',
    platform: 'direct',
    description: 'Internet Archive 音频样本'
  }
]

// 支持的音乐平台配置
export const supportedPlatforms = {
  netease: {
    name: '网易云音乐',
    domains: ['music.163.com'],
    icon: '🎵',
    urlPattern: /music\.163\.com\/song\/media\/outer\/url/,
    requiresProxy: true,
    description: '网易云音乐外链（需要代理）'
  },
  qq: {
    name: 'QQ音乐',
    domains: ['y.qq.com', 'stream.qqmusic.qq.com'],
    icon: '🎶',
    urlPattern: /qq\.com/,
    requiresProxy: true,
    description: 'QQ音乐（需要代理）'
  },
  kugou: {
    name: '酷狗音乐',
    domains: ['kugou.com'],
    icon: '🎼',
    urlPattern: /kugou\.com/,
    requiresProxy: true,
    description: '酷狗音乐（需要代理）'
  },
  kuwo: {
    name: '酷我音乐',
    domains: ['kuwo.cn'],
    icon: '🎤',
    urlPattern: /kuwo\.cn/,
    requiresProxy: true,
    description: '酷我音乐（需要代理）'
  },
  soundcloud: {
    name: 'SoundCloud',
    domains: ['soundcloud.com'],
    icon: '☁️',
    urlPattern: /soundcloud\.com/,
    requiresProxy: true,
    description: 'SoundCloud（需要代理）'
  },
  direct: {
    name: '直接音频文件',
    domains: [],
    icon: '📁',
    urlPattern: /\.(mp3|wav|ogg|flac|m4a|aac)$/i,
    requiresProxy: false,
    description: '直接音频文件链接'
  }
}

/**
 * 检测URL所属的平台
 */
export function detectPlatform(url) {
  if (!url) return null
  
  for (const [key, platform] of Object.entries(supportedPlatforms)) {
    if (platform.urlPattern && platform.urlPattern.test(url)) {
      return key
    }
    
    for (const domain of platform.domains) {
      if (url.includes(domain)) {
        return key
      }
    }
  }
  
  // 默认检查是否为直接音频文件
  if (/\.(mp3|wav|ogg|flac|m4a|aac)$/i.test(url)) {
    return 'direct'
  }
  
  return 'unknown'
}

/**
 * 获取平台信息
 */
export function getPlatformInfo(platformKey) {
  return supportedPlatforms[platformKey] || {
    name: '未知平台',
    icon: '❓',
    requiresProxy: true,
    description: '未知音乐平台'
  }
}

/**
 * 生成代理URL
 */
export function generateProxyUrl(originalUrl) {
  const baseUrl = 'http://localhost:2025/api/audio/stream'
  const encodedUrl = encodeURIComponent(originalUrl)
  return `${baseUrl}?url=${encodedUrl}`
}

/**
 * 检查URL是否需要代理
 */
export function requiresProxy(url) {
  const platform = detectPlatform(url)
  const platformInfo = getPlatformInfo(platform)
  return platformInfo.requiresProxy
}

/**
 * 获取推荐的测试URL
 */
export function getRecommendedTestUrls() {
  return testAudioUrls.filter(item => 
    item.platform === 'direct' // 优先推荐直接可用的URL
  )
}

/**
 * 验证URL格式
 */
export function isValidAudioUrl(url) {
  if (!url || typeof url !== 'string') {
    return false
  }
  
  try {
    new URL(url) // 验证URL格式
    return true
  } catch {
    return false
  }
}

/**
 * 格式化URL显示
 */
export function formatUrlForDisplay(url, maxLength = 50) {
  if (!url) return ''
  
  if (url.length <= maxLength) {
    return url
  }
  
  const start = url.substring(0, maxLength / 2)
  const end = url.substring(url.length - maxLength / 2)
  return `${start}...${end}`
} 